<template>
  <div class="pay-list">
    
    <div class="" v-for="(order,i) in orderList" :key="i">
      <div class="card-header">
        <span class="title">{{ order.create_time }}</span>
        <span class="status">
          <span v-if="order.is_pay">已支付</span>
          <span v-else>未支付</span> |
          <span v-if="order.is_cancel">已取消</span>
          <span v-else @click="onClickOrderCancel(order.id)">取消订单</span>
        </span>
      </div>
      <div @click="onClickShowOrder(order.id)" class="">
        <div class="card-content-inner">
          <div class="cover" v-for="item in order.goods" :key="item.id">
            <div class="img-con">
              <img :src="`${APIBaseURL}${item.goods_img}`" />
            </div>
            <div class="img-tag">x{{ item.count }}</div>
          </div>
        </div>
      </div>
      <p class="amount">
        实付金额：
        <span>¥{{ order.amount }}</span>
      </p>
    </div>
  </div>
</template>

<script>
import {HTTPGetOrderList,HTTPCancleOrder} from "@/api/order.js"
export default {
  data () {
    return {
      orderList: [],
    }
  },
  created () {
    this.getOrderList()
  },
  computed:{
    APIBaseURL(){
      return process.env.VUE_APP_BASE_API
    }
  },
  methods: {
    async getOrderList () {
      try {
        let result = await HTTPGetOrderList();
        if (result.code === 1) {
          this.orderList = result.data; 
          console.log(this.orderList)         
          if(this.orderList.length==0) throw  "订单为空";
        }
        else if(result.code === 2){
          this.$router.push({ name: 'login' })
        }
        else{
          throw result.msg;
        }
      } catch (error) {
        this.$messagebox("错误",error)
      }      
    },//=======================================    
    onClickShowOrder(id) {
      this.$router.push({ name: 'order_show', params: { id: id } });
    },//=======================================
    async onClickOrderCancel (id) {
      try {
        let result = await HTTPCancleOrder({ id });
        if (result.code === 1) {
          this.getOrderList()          
        }
        else if (result.code === 2) {
          this.$router.push({ name: 'login' })
        }
        else{
          throw result.msg;
        }
      } catch (error) {
        this.$messagebox("错误",error);
      } 
    },//=======================================
  }
}
</script>

<style lang="scss" scoped>

.card-header {
  color: #999;
  font-size: 13px;  
  display: flex;
  justify-content: space-between;
  span.title {
    color: #888;
  }
  span.status {
    font-weight: bold;
  }
  
}
.card-content-inner {
  .cover {
    position: relative;
    width: 70px;
    height: 70px;
    display: inline-block;
    margin-right: 10px;
    .img-con {
      width: 100%;
      height: 100%;
      margin-right: 10px;
      border-radius: 2px;
      position: relative;
      img {
        width: 100%;
        height: 100%;
        border-radius: 2px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate3d(-50%, -50%, 0);
      }
    }
    .img-tag {
      position: absolute;
      opacity: 0.5;
      background: #000;
      color: #fff;
      text-align: center;
      font-size: 12px;
      right: 0;
      bottom: 0;
      padding: 0 5px;
      border-radius: 2px 0;
    }
  }
}
.amount {
  padding: 0 10px;
  text-align: right;  
  span {
    color: #151515;
    font-weight: bold;
  }
}
</style>
